/**
 * @(#)FoodCommonManagerImpl.java
 *
 * Copyright (c) 2014-2014  苏州犀牛网络科技有限公司 版权所有
 * xiniunet. All rights reserved.
 *
 * This software is the confidential and proprietary
 * information of  xiniunet.
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with xiniunet.
 */
package com.showcal.thermalcontrol.biz;

import com.showcal.cms.svc.Message;
import com.showcal.foundation.service.FoundationService;
import com.xiniunet.framework.base.BaseManagerImpl;
import com.xiniunet.framework.base.BaseResponse;
import com.xiniunet.framework.exception.ErrorType;
import com.xiniunet.framework.security.Passport;
import com.xiniunet.framework.util.excel.datatable.DataTable;
import com.showcal.foundation.request.IdsGetRequest;
import com.showcal.thermalcontrol.request.*;
import com.showcal.thermalcontrol.response.*;
import com.showcal.thermalcontrol.domain.*;

import com.showcal.thermalcontrol.dal.FoodCommonMapper;
import com.showcal.thermalcontrol.po.FoodCommonPO;
import com.showcal.foundation.service.FoundationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by 顾志雄 on 2015-11-03 11:23:06.
 * @author 顾志雄
 */
@Transactional
@Service("ThermalcontrolFoodCommonManager")
public class FoodCommonManagerImpl extends BaseManagerImpl implements FoodCommonManager {

    @Autowired
    private FoundationService foundationService;

    @Autowired
    private FoodCommonMapper foodCommonMapper;


/**
 * 根据Id获取常见食物
 *
 * @param request 获取常见食物请求
 * @param passport 用户护照
 * @return 获取常见食物应答
 */
@Override
@Transactional(readOnly = true)
public FoodCommonGetResponse get(FoodCommonGetRequest request, Passport passport)
{
    FoodCommonPO entity = foodCommonMapper.getById(request.getId(), passport);
    FoodCommonGetResponse response = new FoodCommonGetResponse();
    if (entity != null) {
    FoodCommon foodCommon = this.getMapper().map(entity, FoodCommon.class);
    response.setFoodCommon(foodCommon );
    }
    else {
        response.addError(ErrorType.EXPECTATION_NULL, Message.COMMON_GET_FAILURE);
    }
    return response;
}


/**
 * 模糊查询常见食物
 *
 * @param request 模糊查询常见食物请求
 * @param passport 用户护照
 * @return 模糊查询常见食物应答
 */
@Override
@Transactional(readOnly = true)
public FoodCommonSearchResponse search(FoodCommonSearchRequest request, Passport passport)
{
    FoodCommonSearchResponse response = new FoodCommonSearchResponse();
    List<FoodCommon> modelList = new ArrayList<>();
    Long count = foodCommonMapper.searchCount(request, passport);

    if (count > 0) {
        // 处理分页参数
        if (request.getPageSize() > 0) {
            //如果输入的页码大于实际的分页数，将页码设置为最后一页的页码
            int lastPageNumber = (int) ((count - 1) / request.getPageSize() + 1);
            if (request.getPageNumber() > lastPageNumber) {
                request.setPageNumber(lastPageNumber);
            }
        }

        //通过关键字查询出用户集合
        List<FoodCommonPO> entityList = foodCommonMapper.search(request, passport);

        for (FoodCommonPO entity : entityList) {
        FoodCommon foodCommon = this.getMapper().map(entity, FoodCommon.class);
        modelList.add(foodCommon);
        }
    }

    response.setTotalCount(count);
    response.setResult(modelList);
    return response;
}

/**
 * 高级查询常见食物
 *
 * @param request 高级查询常见食物请求
 * @param passport 用户护照
 * @return 高级查询应答
 */
@Override
@Transactional(readOnly = true)
public FoodCommonFindResponse find(FoodCommonFindRequest request, Passport passport)
{
    FoodCommonFindResponse response = new FoodCommonFindResponse();
    List<FoodCommon> modelList = new ArrayList<>();
    Long count = foodCommonMapper.findCount(request, passport);
    if (count >0) {
        // 处理分页参数
        if (request.getPageSize() > 0) {
            //如果输入的页码大于实际的分页数，将页码设置为最后一页的页码
            int lastPageNumber = (int) ((count - 1) / request.getPageSize() + 1);
            if (request.getPageNumber() > lastPageNumber) {
                request.setPageNumber(lastPageNumber);
            }
        }


        List<FoodCommonPO> entityList = foodCommonMapper.find(request, passport);
        for (FoodCommonPO entity : entityList) {
            FoodCommon foodCommon = this.getMapper().map(entity, FoodCommon.class);
            modelList.add(foodCommon);
        }

    }

    response.setTotalCount(count);
    response.setResult(modelList);
    return response;
}

/**
 * 获取所有常见食物列表
 *
 * @param request 获取所有常见食物列表请求
 * @param passport 用户护照
 * @return 获取所有常见食物列表应答
 */
@Override
@Transactional(readOnly = true)
public FoodCommonGetAllListResponse getAllList(FoodCommonGetAllListRequest request, Passport passport)
{
    FoodCommonGetAllListResponse response = new FoodCommonGetAllListResponse();


    List<FoodCommonPO> entityList = foodCommonMapper.getAllList(request, passport);


    List<FoodCommon> modelList = new ArrayList<>();
    for (FoodCommonPO entity : entityList) {
    FoodCommon foodCommon = this.getMapper().map(entity, FoodCommon.class);
    modelList.add(foodCommon);
    }

    response.setResult(modelList);

    response.setTotalCount(modelList.size());
    return response;
}


/**
 * 创建常见食物
 *
 * @param request 创建常见食物请求
 * @param passport 用户护照
 * @return 创建常见食物应答
 */
@Override
public FoodCommonCreateResponse create(FoodCommonCreateRequest request, Passport passport)
{
    FoodCommonPO entity = this.getMapper().map(request, FoodCommonPO.class);
    long id = foundationService.getNewId();
    entity.setId(id);

    FoodCommonCreateResponse response = new FoodCommonCreateResponse();

    /* 先检查关键数据是否有重复，在检查通过后才能做插入操作 */
    checkValidate(entity,passport,response);

    if (1 == foodCommonMapper.insert(entity, passport)) {
        response.setId(id);
    }
    else
    {
        response.addError(ErrorType.EXPECTATION_NULL, Message.COMMON_CREATE_FAILURE);
    }
    return response;
}


/**
 * 更新常见食物
 *
 * @param request 更新常见食物请求
 * @param passport 用户护照
 * @return 更新常见食物应答
 */
@Override
public FoodCommonUpdateResponse update(FoodCommonUpdateRequest request, Passport passport)
{
    FoodCommonPO entity = this.getMapper().map(request, FoodCommonPO.class);

    FoodCommonUpdateResponse response = new FoodCommonUpdateResponse();
    Long result=foodCommonMapper.update(entity, passport);
    if (result != 1) {
        response.addError(ErrorType.BUSINESS_ERROR, Message.COMMON_UPDATE_FAILURE);
        return response;
    }
    response.setResult(result);
    return response;
}


/**
 * 删除常见食物
 *
 * @param request 删除常见食物请求
 * @param passport 用户护照
 * @return 删除常见食物应答
 */
@Override
public FoodCommonDeleteResponse delete(FoodCommonDeleteRequest request, Passport passport)
{
 FoodCommonDeleteResponse response = new FoodCommonDeleteResponse();
     Long result= foodCommonMapper.delete(request.getId(), passport);
     response.setResult(result);
    return response;
}




/**
 * 导入常见食物
 *
 * @param request 导入常见食物请求
 * @param passport 用户护照
 * @return 导入常见食物应答
 */
@Override
public FoodCommonListImportResponse importList(FoodCommonListImportRequest request, Passport passport)
{
    DataTable<FoodCommonImport> dataTable = request.getDataTable();

    List<FoodCommonPO> importList = new ArrayList<>();            //需要创建的对象的集合

    List<Long> result = new LinkedList<>();                    //存储ID的集合
    List<FoodCommonImport> beanList = request.getList();            //取出导入对象的集合
    FoodCommonListImportResponse response = new FoodCommonListImportResponse();//最终的返回结果

    /* 批量获取ID */
    IdsGetRequest idsGetRequest = new IdsGetRequest();
    idsGetRequest.setCount(beanList.size());
    List<Long> ids = foundationService.getNewIds(idsGetRequest).getIds();

    int index = 0;
    for (FoodCommonImport importBean : beanList) {
        // 设置
        FoodCommonPO entity = this.getMapper().map(importBean, FoodCommonPO.class);


        importList.add(entity);
        result.add(ids.get(index));         //将取到的ID放入返回结果中
        entity.setId(ids.get(index));       //设置ID

        index++;
    }

    if (!dataTable.hasError()) {
        if (importList.size() > 0) {
            foodCommonMapper.insertBatch(importList, passport);
        }
    }
    else {
        response.setDataTable(dataTable);
        response.addErrors(dataTable.getErrorList());
    }

    response.setList(result);
    return response;

}

    /**
     * 验证对象
     * @param foodCommon 常见食物
     * @param passport 用户护照
     */
    private void checkValidate(FoodCommonPO foodCommon, Passport passport, BaseResponse response) {
        // TODO

    }


}
